package Offer;

import java.util.*;

// 50. 第一个只出现一次的字符
public class Offer50 {
    public char firstUniqChar(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                //如果已经有该字符，则value=-1
                map.replace(s.charAt(i), -1);
            }
            //如果没有该字符，则放入，并且value=i
            else {
                map.put(s.charAt(i), i);
            }
        }
        //返回value最小的值对应的key
        for (int i = 0; i < s.length(); i++) {
            if (map.containsValue(i)) {
                Set<Map.Entry<Character, Integer>> entries = map.entrySet();
                Iterator<Map.Entry<Character, Integer>> iterator = entries.iterator();
                while (iterator.hasNext()) {
                    Map.Entry<Character, Integer> next = iterator.next();
                    Integer value = next.getValue();
                    if (i == value) {
                        return next.getKey();
                    }
                }
            }
        }
        return ' ';
    }
}
